N <- 1000

b<-matrix(1,N,1)
bL<-sqrt(sum(b^2))

j1<-b
for(i in 1:(N*0.2)){#R(0)=0.6
	j1[i]=-1
}

j2<-b
for(i in ((N*0.2)+1):(N*0.6)){#R(0)=0.2
#for(i in 1:(N*0.2)){
	j2[i]=-1
}

step <- function(x) ifelse(0 < x, 1, 0)

set.seed(300)
XRandSd <- 1/sqrt(N)
XRandM <- 0

C_DELTA <- 0.1
SMPL_SETs <- round(1/C_DELTA)
SMPLs <- 100

Eg<-matrix(0,(SMPL_SETs+1)*(SMPL_SETs+1),3)

pboost <- function(){
	x<-matrix(rnorm(N*SMPLs,XRandM,XRandSd),N,SMPLs)
	for (i in 0:SMPL_SETs){
		c1<-i*C_DELTA
		for (j in 0:SMPL_SETs){
			c2<-j*C_DELTA
			
			eg<-0.0
			for (t in 1:SMPLs){
				uj1<-sum(x[,t]*j1)
				uj2<-sum(x[,t]*j2)
				ub<-sum(x[,t]*b)
				eg<-(eg+step(-ub*(c1*uj1+c2*uj2)))
			}
			Eg[(i*(SMPL_SETs+1)+j+2),1] <<- c1
			Eg[(i*(SMPL_SETs+1)+j+2),2] <<- c2
			Eg[(i*(SMPL_SETs+1)+j+2),3] <<- eg/SMPLs
		}
	}
}

system.time(#returns CPU Time Used
	#MAIN
	pboost()
,FALSE)

#setwd("~/Documents/lab2006/dat")	#for OSX
#dat=cbind(L,R,Rbg,q)
#dat=cbind(L,R,Rbg,Rpb,q)
dat<-Eg
write.table(dat,format(Sys.time(),"PercML-PB-%Y%m%d-%H%M%S.txt"), sep="\t",row.names=F,col.names=F)
print(Eg)
#
#plot(Eg[,1],Eg[,2],xlim=c(0.,1),ylim=c(0.,1),type="l")#;par(new=T)